#include "qnmsettings.h"
#include "utils.h"

QNMSettings::QNMSettings(QObject *parent) : QSettings( Utils::getFileConfig(), QSettings::NativeFormat, parent)
{
    group_main = "main";
    key_language = "language";
    key_orientation = "orientation";
    key_datatimeformat = "datatime";
    key_theme = "theme";

    group_monitor = "monitor";
    key_graph_type = "graph_type";
    key_show_marker = "show_marker";
    key_show_rx = "show_rx";
    key_show_tx = "show_tx";
    key_timestamp = "timestamp";
    key_color_rx = "color_rx";
    key_color_tx = "color_tx";
    key_refresh_interval = "refresh_interval";
    key_iface_monitor = "iface_monitor";
    key_list_ifaces = "ifaces_list";
}

/*******************************************************************/
/*---------*/
void QNMSettings::setTypeGraphStat()
{
    this->beginGroup( group_monitor );
    this->setValue( key_graph_type, QVariant("0") );
    this->endGroup();
}

void QNMSettings::setTypeGraphTime()
{
    this->beginGroup( group_monitor );
    this->setValue( key_graph_type, QVariant("1") );
    this->endGroup();
}

int QNMSettings::getTypeGraph()
{
    this->beginGroup( group_monitor );
    QVariant qv = this->value( key_graph_type, QVariant("0") );
    this->endGroup();
    int r = qv.toInt();
    return r;
}
/*---------*/

void QNMSettings::showMarker(const bool isShow)
{
    this->beginGroup( group_monitor );
    this->setValue( key_show_marker, QVariant(isShow) );
    this->endGroup();
}

bool QNMSettings::isShowMarker()
{
    this->beginGroup( group_monitor );
    QVariant qv = this->value( key_show_marker, QVariant( false ) );
    this->endGroup();
    bool r = qv.toBool();
    return r;
}
/*---------*/

void QNMSettings::showRX(const bool isShow)
{
    this->beginGroup( group_monitor );
    this->setValue( key_show_rx, QVariant(isShow) );
    this->endGroup();
}

bool QNMSettings::isShowRX()
{
    this->beginGroup( group_monitor );
    QVariant qv = this->value( key_show_rx, QVariant( true ) );
    this->endGroup();
    bool r = qv.toBool();
    return r;
}
/*---------*/

void QNMSettings::showTX(const bool isShow)
{
    this->beginGroup( group_monitor );
    this->setValue( key_show_tx, QVariant(isShow) );
    this->endGroup();
}

bool QNMSettings::isShowTX()
{
    this->beginGroup( group_monitor );
    QVariant qv = this->value( key_show_tx, QVariant( true ) );
    this->endGroup();
    int r = qv.toBool();
    return r;
}
/*---------*/

void QNMSettings::setTimestamp(const int ts)
{
    this->beginGroup( group_monitor );
    this->setValue( key_timestamp, QVariant(ts) );
    this->endGroup();
}

int QNMSettings::getTimestamp()
{
    this->beginGroup( group_monitor );
    QVariant qv = this->value( key_timestamp, QVariant( 30 ) );
    this->endGroup();
    int r = qv.toInt();
    return r;
}

/*---------*/

void QNMSettings::setRefreshInterval(const int ri)
{
    this->beginGroup( group_monitor );
    this->setValue( key_refresh_interval, QVariant(ri) );
    this->endGroup();
}

int QNMSettings::getRefreshInterval()
{
    this->beginGroup( group_monitor );
    QVariant qv = this->value( key_refresh_interval, QVariant( 1 ) );
    this->endGroup();
    int r = qv.toInt();
    return r;
}
/*---------*/

void QNMSettings::setIfaceMonitor(const QString& iface)
{
    this->beginGroup( group_monitor );
    this->setValue( key_iface_monitor, QVariant(iface) );
    this->endGroup();
}

QString QNMSettings::getIfaceMonitor()
{
    this->beginGroup( group_monitor );
    QVariant qv = this->value( key_iface_monitor, QVariant( "gprs0" ) );
    this->endGroup();
    QString r = qv.toString();
    return r;
}
/*---------*/

void QNMSettings::setColorRX(const QString& color)
{
    this->beginGroup( group_monitor );
    this->setValue( key_color_rx, QVariant(color) );
    this->endGroup();
}

QString QNMSettings::getColorRX()
{
    this->beginGroup( group_monitor );
    QVariant qv = this->value( key_color_rx, QVariant( "green" ) );
    this->endGroup();
    QString r = qv.toString();
    return r;
}
/*---------*/

void QNMSettings::setColorTX(const QString& color)
{
    this->beginGroup( group_monitor );
    this->setValue( key_color_tx, QVariant(color) );
    this->endGroup();
}

QString QNMSettings::getColorTX()
{
    this->beginGroup( group_monitor );
    QVariant qv = this->value( key_color_tx, QVariant( "blue" ) );
    this->endGroup();
    QString r = qv.toString();
    return r;
}
/*---------*/

void QNMSettings::setListIfaces(const QStringList& listIfaces)
{
    this->beginGroup( group_monitor );
    this->setValue( key_list_ifaces, QVariant(listIfaces) );
    this->endGroup();
}

QStringList QNMSettings::getListIfaces()
{
    QStringList defaultValue;
    defaultValue.append("gprs0");
    defaultValue.append("wlan0");
    defaultValue.append("eth0");
    defaultValue.append("usb0");

    this->beginGroup( group_monitor );
    QVariant qv = this->value( key_list_ifaces, QVariant( defaultValue ) );
    this->endGroup();
    QStringList r = qv.toStringList();
    return r;
}
/*---------*/

/*******************************************************************/

void QNMSettings::setDataTimeFormat(const QString& dt_format)
{
    this->beginGroup( group_main );
    this->setValue( key_datatimeformat, QVariant(dt_format) );
    this->endGroup();
}

QString QNMSettings::getDataTimeFormat()
{
    this->beginGroup( group_main );
    QVariant qv = this->value( key_datatimeformat, QVariant( "yyyy-MM-dd HH:mm:ss" ) );
    this->endGroup();
    QString r = qv.toString();
    return r;
}

/*---------*/
void QNMSettings::setLanguage(const QString &indx)
{
    this->beginGroup( group_main );
    this->setValue( key_language, QVariant(indx) );
    this->endGroup();
}

QString QNMSettings::getLanguage()
{
    this->beginGroup( group_main );
    QVariant qv = this->value( key_language, QVariant( "0" ) );
    this->endGroup();
    QString r = qv.toString();
    return r;
}
/*---------*/


/*---------*/
void QNMSettings::setTheme(const QString &theme)
{
    this->beginGroup( group_main );
    this->setValue( key_theme, QVariant(theme) );
    this->endGroup();
}

QString QNMSettings::getTheme()
{
    this->beginGroup( group_main );
    QVariant qv = this->value( key_theme, QVariant( "dark" ) );
    this->endGroup();
    QString r = qv.toString();
    return r;
}
/*---------*/


void QNMSettings::setOrientation(const QString &theme)
{
    this->beginGroup( group_main );
    this->setValue( key_orientation, QVariant(theme) );
    this->endGroup();
}

QString QNMSettings::getOrientation()
{
    this->beginGroup( group_main );
    QVariant qv = this->value( key_orientation, QVariant( "auto" ) );
    this->endGroup();
    QString r = qv.toString();
    return r;
}
/*---------*/
